#pragma once
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstdlib>
#include "Common.hpp"
#include "Log.hpp"
#include "InetAddr.hpp"
namespace SocketModule
{
    using namespace LogModule;
    const static int gbacklog = 16;
    class Socket
    {
    public:
        virtual ~Socket() {}
        virtual void SocketOrDie() = 0;
        virtual void BindOrDie(uint16_t port) = 0;
        virtual void ListenOrDie(int backlog) = 0;
        virtual std::shared_ptr<Socket> Accept(InetAddr *client) = 0;
        virtual void Close() = 0;
        virtual int Recv(std::string *out) = 0;
        virtual int Send(const std::string &message) = 0;
        virtual int Connect(const std::string &server_ip, uint16_t port) = 0;
        void BuildTcpSocketMetond(uint16_t port, int backlog = gbacklog)
        {
            SocketOrDie();
            BindOrDie(port);
            ListenOrDie(backlog);
        }
        void BuildTcpClientSocketMethod()
        {
            SocketOrDie();
        }
    };
    class TcpSocket : public Socket
    {
        public:
        TcpSocket()
            : _sockfd(-1)
        {
        }
        TcpSocket(int fd)
            : _sockfd(fd)
        {
        }
        ~TcpSocket() = default;
        void SocketOrDie() override
        {
            _sockfd = socket(AF_INET,SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                LOG(LogLevel::ERROR) << "socket error";
                exit(SOCKET_ERR);
            }
            LOG(LogLevel::INFO) << "socket sucuess";
        }
        void BindOrDie(uint16_t port) override
        {
            InetAddr addr(port);
            int n = bind(_sockfd, addr.NetAddPtr(), addr.NetAddlen());
            if (n < 0)
            {
                LOG(LogLevel::ERROR) << "bind error";
                exit(BIND_ERR);
            }
            LOG(LogLevel::INFO) << "bind sucuess";
        }

        void ListenOrDie(int backlog)
        {
            int n = listen(_sockfd, backlog);
            if (n < 0)
            {
                LOG(LogLevel::ERROR) << "listen error";
                exit(LISTEN_ERR);
            }
            LOG(LogLevel::INFO) << "listen sucuess";
        }
        std::shared_ptr<Socket> Accept(InetAddr *client)
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int fd = accept(_sockfd, (sockaddr *)&peer, &len);
            if (fd < 0)
            {
                LOG(LogLevel::WARNING) << "accept error";
                return nullptr;
            }
            client->SetAddr(peer);
            return std::make_shared<TcpSocket>(fd);
        }

        void Close()
        {
            if (_sockfd > 0)
                ::close(_sockfd);
        }

        int Recv(std::string *out) override
        {
            char buffer[1024];
            ssize_t n = ::recv(_sockfd, buffer, sizeof(buffer) - 1, 0);
            if (n > 0)
            {
                buffer[n] = 0;
                *out += buffer;
            }
            return n;
        }
        int Send(const std::string &message)
        {
            return send(_sockfd, message.c_str(), message.size(), 0);
        }
        int Connect(const std::string &server_ip, uint16_t port)
        {
            InetAddr server(server_ip, port);
            return ::connect(_sockfd, server.NetAddPtr(), server.NetAddlen());
        }

    private:
        int _sockfd;
    };

}